package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.entity.Arrive;
import com.lovelyj.trip_by_train.entity.DailyTrain;
import com.lovelyj.trip_by_train.entity.Train;
import com.lovelyj.trip_by_train.service.ArriveService;
import com.lovelyj.trip_by_train.service.DailyTrainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/arrive")
public class ArriveController {

    @Autowired
    private ArriveService arriveService;

    @Autowired
    private DailyTrainService dailyTrainService;

    private boolean ifInDaily(String trainNo) {
        LambdaQueryWrapper<DailyTrain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrain::getTrainNum, trainNo);
        return dailyTrainService.count(queryWrapper) > 0;
    }

    @GetMapping("/select")
    public R<List<Arrive>> select(HttpServletRequest request, String trainNum) {
        LambdaQueryWrapper<Arrive> queryWrapper = new LambdaQueryWrapper<Arrive>();
        queryWrapper.eq(Arrive::getTrainNum, trainNum);
        queryWrapper.orderByAsc(Arrive::getArriveDay).orderByAsc(Arrive::getArriveTime);
        List<Arrive> list = arriveService.list(queryWrapper);
        return R.success(list);
    }

    @PutMapping("/create")
    public R<String> create(HttpServletRequest request,@RequestBody Map<String,Object> map) {
        log.info("arrive={}", map.toString());

        String trainNum = map.get("trainNum").toString();


        if(ifInDaily(trainNum)){
            return R.error("车次在daily中！不可修改");
        }


        Arrive arrive = new Arrive();
        arrive.setTrainNum(map.get("trainNum").toString());
        arrive.setStation(map.get("station").toString());
        arrive.setArriveDay(Integer.parseInt(map.get("arriveDay").toString()));
        arrive.setDepartureDay(Integer.parseInt(map.get("departureDay").toString()));
        arrive.setDistance(Integer.parseInt(map.get("distance").toString()));
        String sTime = map.get("arriveTime").toString();
        String eTime = map.get("departureTime").toString();
        sTime=sTime.replace("Z", "");
        eTime=eTime.replace("Z", "");
        String[] time = sTime.split("T");
        LocalTime startTime = LocalTime.parse(time[1]).plusHours(8);
        log.info(startTime.toString());
        time = eTime.split("T");
        LocalTime endTime = LocalTime.parse(time[1]).plusHours(8);;
        log.info(endTime.toString());
        arrive.setArriveTime(startTime);
        arrive.setDepartureTime(endTime);
        log.info("arrive={}", arrive);
        arriveService.save(arrive);
        return R.success("success");
    }

    @PutMapping("/update")
    public R<String> update(HttpServletRequest request,@RequestBody Map<String,Object> map) {
        log.info("arrive={}", map.toString());

        String trainNum = map.get("trainNum").toString();

        if(ifInDaily(trainNum)){
            return R.error("车次在daily中！不可修改");
        }

        Arrive arrive = new Arrive();
        arrive.setTrainNum(map.get("trainNum").toString());
        arrive.setStation(map.get("station").toString());
        arrive.setArriveDay(Integer.parseInt(map.get("arriveDay").toString()));
        arrive.setDepartureDay(Integer.parseInt(map.get("departureDay").toString()));
        arrive.setDistance(Integer.parseInt(map.get("distance").toString()));
        String sTime = map.get("arriveTime").toString();
        String eTime = map.get("departureTime").toString();
        sTime=sTime.replace("Z", "");
        eTime=eTime.replace("Z", "");
        String[] time = sTime.split("T");
        LocalTime startTime = LocalTime.parse(time[1]).plusHours(8);
        log.info(startTime.toString());
        time = eTime.split("T");
        LocalTime endTime = LocalTime.parse(time[1]).plusHours(8);;
        log.info(endTime.toString());
        arrive.setArriveTime(startTime);
        arrive.setDepartureTime(endTime);
        arrive.setId(Long.parseLong(map.get("id").toString()));
        log.info("arrive={}", arrive);
        arriveService.updateById(arrive);
        return R.success("success");
    }

    @PostMapping("/deleteByIds")
    public R<String> deleteByIds(HttpServletRequest request,@RequestBody List<Long> selectedIds){

        log.info("deleteByIds...");

        String trainNum = arriveService.getById(selectedIds.get(0)).getTrainNum();

        if(ifInDaily(trainNum)){
            return R.error("车次在daily中！不可修改");
        }

        arriveService.removeByIds(selectedIds);

        return R.success("删除成功");
    }

    @DeleteMapping
    public R<String> deleteById(HttpServletRequest request,Long id){
        log.info("deleteById..."+id);

        String trainNum = arriveService.getById(id).getTrainNum();

        if(ifInDaily(trainNum)){
            return R.error("车次在daily中！不可修改");
        }

        arriveService.removeById(id);
        return R.success("删除成功");
    }

}
